package com.example.bootcurriculumproject.controller;

import com.example.bootcurriculumproject.entity.Cart;
import com.example.bootcurriculumproject.service.CartService;
import com.example.bootcurriculumproject.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private RedisUtil redisUtil;
    //添加商品到购物车
    @PostMapping("/add")
    @ResponseBody
    @CrossOrigin
    public boolean add(@RequestBody Cart cart){
        cartService.cartAdd(cart);
        return true;
    }
    //从购物车删除单项商品
    @DeleteMapping("/deleteOne/{num}")
    @ResponseBody
    @CrossOrigin
    public boolean deleteOne(@PathVariable("num") String cartNum){
        //http://localhost:8080/cart/deleteOne/PK105085uyyz
        cartService.cartRemove(cartNum);
        return true;
    }
    //清空购物车(当提交订单后，购物车信息清空，购物车内的商品自动以单个商品为一个订单自动插入)
    @DeleteMapping("/clean/{ids}")
    @ResponseBody
    @CrossOrigin
    public boolean clean(@PathVariable("ids") String[] ids){
        //http://localhost:8080/cart/clean/PK162562xboe,PK281989ebzv
     for(String id:ids){
         cartService.cartRemove(id);
     }
     return true;
    }
    //获取全部购物车信息
    @GetMapping("/find")
    @ResponseBody
    @CrossOrigin
    public List selectAll(@RequestBody String uid){
        http://localhost:8080/cart/find?uid=7788
//          String uid = String.valueOf(redisUtil.get("nowUid"));
        return cartService.selectCart(uid);
    }
    //购物车所有商品提交
    @PostMapping("/submit")
    @ResponseBody
    @CrossOrigin
    public boolean submitCart(@RequestBody List<Cart> list){
       cartService.submit(list);
       return true;
    }
}
